package net.oschina.app.fragment;

import net.oschina.app.R;
import net.oschina.app.adapter.FriendSelectAdapter;
import net.oschina.app.base.ListBaseAdapter;
import net.oschina.app.bean.Friend;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class SelectFriendsFragment extends FriendsFragment {

    private static final int MAX_SELECT_NUMBER = 10;

    FriendSelectAdapter mFriendSelectAdapter;
    TextView mSelectNumberView;
    MenuItem mSelectNumberMenuItem;

    FriendSelectAdapter.OnCheckItemListener mOnCheckItemListener =
            new FriendSelectAdapter.OnCheckItemListener() {
                @Override
                public boolean onCheckItem(FriendSelectAdapter adapter, Friend friend,
                        FriendSelectAdapter.ViewHolder views, boolean willCheck) {
                    final int checkedItemsNumber = adapter.getCheckedItemsNumber();
                    if (willCheck) {
                        if (checkedItemsNumber == MAX_SELECT_NUMBER) {
                            return false;
                        } else {
                            bindSelectNumberState(checkedItemsNumber + 1);
                            return true;
                        }
                    } else {
                        bindSelectNumberState(checkedItemsNumber - 1);
                        return true;
                    }
                }
            };

    public static Bundle buildArguments(Integer catalog, Integer uid) {
        Bundle arguments = new Bundle();
        if (catalog != null) {
            arguments.putInt(BUNDLE_KEY_CATALOG, catalog);
        }
        if (uid != null) {
            arguments.putInt(BUNDLE_KEY_UID, uid);
        }
        return arguments;
    }

    public static SelectFriendsFragment newInstance(Integer catalog, Integer uid) {
        SelectFriendsFragment fragment = new SelectFriendsFragment();
        fragment.setArguments(buildArguments(catalog, uid));
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @LayoutRes
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_friends_select;
    }

    @Override
    protected ListBaseAdapter<Friend> getListAdapter() {
        if (mFriendSelectAdapter == null) {
            mFriendSelectAdapter = new FriendSelectAdapter();
        }
        mFriendSelectAdapter.setOnCheckItemListener(mOnCheckItemListener);
        bindSelectNumberState(mFriendSelectAdapter.getCheckedItemsNumber());
        return mFriendSelectAdapter;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.select_friends_menu, menu);
        mSelectNumberMenuItem = menu.findItem(R.id.action_select_friends_number);
        mSelectNumberView = (TextView) MenuItemCompat
                .getActionView(mSelectNumberMenuItem)
                .findViewById(R.id.select_friends_numbers);
        bindSelectNumberState(mFriendSelectAdapter.getCheckedItemsNumber());
    }

    @Override
    public void onDestroyOptionsMenu() {
        mSelectNumberView = null;
        mSelectNumberMenuItem = null;
        super.onDestroyOptionsMenu();
    }

    private void bindSelectNumberState(int selectNumber) {
        if (selectNumber < 0 || selectNumber > MAX_SELECT_NUMBER) {
            String message = "selectNumber < 0 || selectNumber > MAX_SELECT_NUMBER";
            message += "\nselectNumber: " + selectNumber;
            message += "\nMAX_SELECT_NUMBER:" + MAX_SELECT_NUMBER;
            throw new IllegalArgumentException(message);
        }
        final String title =
                getString(R.string.actionbar_view_select_friends, selectNumber, MAX_SELECT_NUMBER);
        if (mSelectNumberView != null) {
            mSelectNumberView.setText(title);
        }
        if (mSelectNumberMenuItem != null) {
            mSelectNumberMenuItem.setTitle(title);
        }
    }

}
